package cn.ajb.filemanager.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.ajb.filemanager.dao.SysParamDictDao;
import cn.ajb.filemanager.entity.SysParamDict;
import cn.ajb.filemanager.util.Contant;
import cn.ajb.filemanager.util.JSONResult;



@Service
@Transactional
public class SysParamDictService {

	@Autowired
	private SysParamDictDao sysParamDictDao ;
	@Transactional(readOnly=true)
	public List<SysParamDict> selectSysParamDictlist(){
		return sysParamDictDao.selectSysParamDictlist();
	}
	
	@Transactional(readOnly=true)
	public List<SysParamDict> selectSysParamDictByType(Integer type) {
		return sysParamDictDao.selectSysParamDictByType(type);
	}
	
	
	public JSONResult<String> updateSysParam(String type,String[] names){
		if(Contant.UPLOAD_USER.equals(type)){
			updateSysParam(10,names);
		}else if(Contant.SYS_TYPE.equals(type)){
			updateSysParam(1,names);
		}else if(Contant.PRODUCT_TYPE.equals(type)){
			updateSysParam(3,names);
		}else if(Contant.PRODUCT_MODEL.equals(type)){
			updateSysParam(4,names);
		}else if(Contant.PRODUCT_NAME.equals(type)){//
			updateSysParam(2,names);
		}else if(Contant.VERSION.equals(type)){
			updateSysParam(7,names);
		}else if(Contant.USAGE_SCENARIOS.equals(type)){
			updateSysParam(5,names);
		}else if(Contant.SPECIAL_NUM.equals(type)){
			updateSysParam(9,names);
		}else if(Contant.DOCUMENT_TYPE.equals(type)){
			updateSysParam(6,names);
		}else if(Contant.MATERIEL.equals(type)){
			updateSysParam(8,names);
		}
		JSONResult<String> jsonResult = new JSONResult<String>();
		jsonResult.setMsg("修改文档属性成功");
		jsonResult.setStatus(JSONResult.STATUS_SUCCESS);
		return jsonResult ;
	}
	private void updateSysParam(Integer type ,String[] names){
		List<SysParamDict> list = new ArrayList<SysParamDict>();
		for (String name : names) {
			if(name!=null && !"".equals(name)){
				SysParamDict sysParamDict = new SysParamDict();
				sysParamDict.setName(name);
				sysParamDict.setType(type);
				list.add(sysParamDict);
			}
		}
		sysParamDictDao.delSysParamByType(type);
		sysParamDictDao.insertSysParam(list);
	}
}
